Repository navigation
Fix dangling V8 handles in v8ToChoc object conversion with V8 15.4+ - #114
Closed
danielraffel wants to merge 1 commit into
Closed
danielraffel wants to merge 1 commit into
danielraffel wants to merge 1 commit into
Conversation
V8Context::v8ToChoc walked an object's property names with v8::Array::Iterate and stored the v8::Local values returned by Get() inside the callback, converting them after Iterate returned. Since V8 15.4 (V8 commit 3186b30e34b4) Iterate runs the callback inside its own HandleScope, so those Locals are released when Iterate returns and later conversions read freed handle slots, crashing or producing garbage. V8 also documents that the Iterate callback must not allocate or call back into V8, which Get() can do. Use a plain index loop over the property names instead.
Collaborator
|
Thanks! I've had a look at this now and pushed some changes that should do the same thing a bit more efficiently, and also sort out the GetIsolate() issue - let me know if you have any problems with it! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What breaks
With V8 15.4 and later, converting a JS object to a
choc::value::ValuethroughV8Context(for example the result ofevaluateExpressionon an object literal with several members) can return garbage for some members or crash. We see SIGBUS insidev8::internal::KeyAccumulator::CollectOwnElementIndiceswith V8 15.7.112.5; the same code works with 15.3.Why
V8Context::v8ToChocwalks the property names withv8::Array::Iterate, callso->Get (c, key)inside the callback, and stores the returnedv8::Localvalues inpropValues, converting them afterIteratereturns.Since V8 15.4 (V8 commit 3186b30e34b4, "Expect callback passed to Array::Iterate() may potentially cause a GC"),
FastIterateArrayinsrc/api/api.ccruns the callback inside aHandleScopethat V8 opens itself. The Locals created in the callback are released whenIteratereturns, sopropValuesholds dangling handles. The next conversion reuses those slots and later members read whatever was written there.Independently of that change, the
Array::Iteratedocumentation already says the callback must not allocate or call back into V8, andGet()can run a getter.Fix
Replace the
Iteratecall with a plain index loop over the property-name array, so the Locals live in the caller's scope. The now-unusedIteratorTypealias is removed.Standalone repro (plain V8, same pattern)
Reports dangling handles with V8 15.7.112.5 and none with 15.3.
Separately, and not addressed here: current choc main does not compile against V8 15.4+ headers at all, because the two module-loading callbacks call
v8::Context::GetIsolate(), which those headers no longer provide. With those two calls replaced locally, the patched header compiles cleanly (-std=c++20 -fsyntax-only -Wall -Wextra) against the V8 15.4 and 15.7 headers.